summaryrefslogtreecommitdiff
path: root/app/api/data-room/[projectId]/[fileId]/route.ts
blob: 5e6d508829c8d0c2ce2c755f4459a851f1d67d22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// app/api/data-room/[projectId]/[fileId]/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth/next';
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
import { FileService, type FileAccessContext } from '@/lib/services/fileService';
import { fileActivityLogs, fileItems } from '@/db/schema';
import { and, eq, sql } from 'drizzle-orm';
import db from "@/db/db"

// 파일 정보 조회
export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ projectId: string; fileId: string }> }
) {
  try {
    const { projectId, fileId } = await params;
    
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: '인증이 필요합니다' }, { status: 401 });
    }

    const context: FileAccessContext = {
      userId: Number(session.user.id),
      userDomain: session.user.domain || 'partners',
      userEmail: session.user.email,
      ipAddress: request.ip || request.headers.get('x-forwarded-for') || undefined,
      userAgent: request.headers.get('user-agent') || undefined,
    };

    const fileService = new FileService();
    const hasAccess = await fileService.checkFileAccess(
      fileId,
      context,
      'view'
    );

    if (!hasAccess) {
      return NextResponse.json(
        { error: '파일 접근 권한이 없습니다' },
        { status: 403 }
      );
    }

    // 파일 정보 반환
    const file = await fileService.downloadFile(fileId, context);
    
    if (!file) {
      return NextResponse.json(
        { error: '파일을 찾을 수 없습니다' },
        { status: 404 }
      );
    }

    return NextResponse.json(file);
  } catch (error) {
    console.error('파일 조회 오류:', error);
    return NextResponse.json(
      { error: '파일 조회에 실패했습니다' },
      { status: 500 }
    );
  }
}

// 파일 수정 (이름 변경, 카테고리 변경, 파일 이동 통합)
export async function PATCH(
  request: NextRequest,
  { params }: { params: Promise<{ projectId: string; fileId: string }> }
) {
  try {
    const { projectId, fileId } = await params;
    
    console.log('PATCH 요청 받음:', { projectId, fileId });
    
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: '인증이 필요합니다' }, { status: 401 });
    }

    const body = await request.json();
    const { name, category, parentId, applyToChildren } = body;
    
    console.log('요청 본문:', { name, category, parentId, applyToChildren });

    // 권한 확인
    const fileItem = await db
      .select()
      .from(fileItems)
      .where(
        and(
          eq(fileItems.id, fileId),
          eq(fileItems.projectId, projectId)
        )
      )
      .limit(1);

    if (!fileItem[0]) {
      return NextResponse.json({ error: 'File not found' }, { status: 404 });
    }

    // 내부 사용자만 수정 가능
    if (session.user.domain === 'partners') {
      return NextResponse.json({ error: 'Permission denied' }, { status: 403 });
    }

    const updateData: any = {
      updatedBy: session.user.id,
      updatedAt: new Date(),
    };

    // 이름 변경 처리
    if (name !== undefined) {
      // 파일인 경우 확장자 유지
      if (fileItem[0].type === 'file') {
        const originalExt = fileItem[0].name.lastIndexOf('.');
        const newNameWithoutExt = name.lastIndexOf('.') > -1 
          ? name.substring(0, name.lastIndexOf('.'))
          : name;
        const ext = originalExt > -1 ? fileItem[0].name.substring(originalExt) : '';
        updateData.name = newNameWithoutExt + ext;
      } else {
        updateData.name = name;
      }

      // 경로 업데이트 (하위 항목들도 업데이트 필요)
      if (fileItem[0].type === 'folder') {
        const oldPath = fileItem[0].path + fileItem[0].name + '/';
        const newPath = fileItem[0].path + updateData.name + '/';
        
        // 하위 항목들의 경로 업데이트
        await db
          .update(fileItems)
          .set({ 
            path: sql`REPLACE(path, ${oldPath}, ${newPath})` 
          })
          .where(
            and(
              eq(fileItems.projectId, projectId),
              sql`path LIKE ${oldPath + '%'}`
            )
          );
      }
    }

    // 카테고리 변경 처리
    if (category !== undefined) {
      updateData.category = category;

      // 폴더인 경우 하위 항목들도 같은 카테고리로 변경할지 옵션 제공
      if (fileItem[0].type === 'folder' && applyToChildren) {
        await updateChildrenCategory(fileId, category, projectId);
      }
    }

    // 부모 폴더 변경 (파일 이동) 처리
    if (parentId !== undefined) {
      updateData.parentId = parentId;
      
      // 새 부모 폴더의 경로 가져오기
      if (parentId) {
        const parentFolder = await db
          .select()
          .from(fileItems)
          .where(eq(fileItems.id, parentId))
          .limit(1);
        
        if (parentFolder[0]) {
          updateData.path = parentFolder[0].path + parentFolder[0].name + '/';
        }
      } else {
        updateData.path = '/';
      }
    }

    console.log('업데이트 데이터:', updateData);

    // 업데이트 실행
    const [updatedItem] = await db
      .update(fileItems)
      .set(updateData)
      .where(eq(fileItems.id, fileId))
      .returning();

    // 활동 로그 기록
    const action = name ? 'rename' : category ? 'category_change' : 'move';
    const actionDetails: any = {};
    
    if (name) {
      actionDetails.from = fileItem[0].name;
      actionDetails.to = updateData.name;
    } else if (category) {
      actionDetails.from = fileItem[0].category;
      actionDetails.to = category;
    } else if (parentId !== undefined) {
      actionDetails.from = fileItem[0].parentId;
      actionDetails.to = parentId;
    }

    await db.insert(fileActivityLogs).values({
      fileItemId: fileId,
      projectId,
      action,
      actionDetails,
      userId: Number(session.user.id),
      userEmail: session.user.email,
      userDomain: session.user.domain,
    });

    console.log('업데이트 완료:', updatedItem);

    return NextResponse.json(updatedItem);
  } catch (error) {
    console.error('파일 수정 오류:', error);
    return NextResponse.json(
      { error: '파일 수정에 실패했습니다', details: error.message },
      { status: 500 }
    );
  }
}

// 파일 삭제
export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ projectId: string; fileId: string }> }
) {
  try {
    const { projectId, fileId } = await params;
    
    const session = await getServerSession(authOptions);
    if (!session?.user) {
      return NextResponse.json({ error: '인증이 필요합니다' }, { status: 401 });
    }

    const context: FileAccessContext = {
      userId: session.user.id,
      userDomain: session.user.domain || 'partners',
      userEmail: session.user.email,
      ipAddress: request.ip || request.headers.get('x-forwarded-for') || undefined,
      userAgent: request.headers.get('user-agent') || undefined,
    };

    const fileService = new FileService();
    await fileService.deleteFile(fileId, context);

    return NextResponse.json({ success: true });
  } catch (error) {
    if (error instanceof Error && error.message.includes('권한')) {
      return NextResponse.json(
        { error: error.message },
        { status: 403 }
      );
    }

    console.error('파일 삭제 오류:', error);
    return NextResponse.json(
      { error: '파일 삭제에 실패했습니다' },
      { status: 500 }
    );
  }
}

// 하위 항목 카테고리 업데이트 함수
async function updateChildrenCategory(
  parentId: string, 
  category: string,
  projectId: string
) {
  const children = await db
    .select()
    .from(fileItems)
    .where(
      and(
        eq(fileItems.parentId, parentId),
        eq(fileItems.projectId, projectId)
      )
    );

  for (const child of children) {
    await db
      .update(fileItems)
      .set({ category })
      .where(eq(fileItems.id, child.id));

    // 재귀적으로 하위 폴더 처리
    if (child.type === 'folder') {
      await updateChildrenCategory(child.id, category, projectId);
    }
  }
}